
;;================================================================
;; Routine to make various analysis plots in support of the IRDC
;;   distance modeling code.


;; Define the COMMON BLOCK
COMMON FFORE_BLOCK,n,rb3,R0,d,R,Z,tau,f_data,sig_f,corr,farlist,do_Tfit,$
   lpstr,rho_hi,rho_h2,rho_star

;; Since we are using routines buried in BGPS_FFORE, compile now to
;; they are available.
FORWARD_FUNCTION BGPS_FFORE_MODEL, BGPS_FFORE_LOOPIE
RESOLVE_ROUTINE, 'BGPS_FFORE', /COMPILE_FULL_FILE, /EITHER

;; Get galactic params
defsysv, '!MW', exists = exists
IF NOT exists THEN galactic_params 
;; Using !MW parameters -- Goes into COMMON block!
R0 = !MW.R0

restore,'./irdc_dist_model/bgps_rb3.sav',/ver
restore,'./irdc_dist_model/data/case4_irdc_TdFIT.sav',/ver
restore,'./irdc_dist_model/fitdist.sav',/ver

;; Define arrays for TEMP_DEPENDENT_VARIABLES
n = n_elements(rb3)
tau    = dblarr(n)
f_data = dblarr(n)
sig_f  = dblarr(n)

plotsym,0,0.5,/fill


;; Get sources in funny range
funny = WHERE(fitdist/1.d3 GE (-10.*rb3.s_peak/1.d3 + 20.), nf)
print,'# Funny: ',nf

;;======================================================
;; Page 1 of plots

myps,'irdc_dist_model/analysis_plots/misc_analysis_plots.eps',xsize=15,ysize=9
multiplot,[3,2],gap=0.04,/doxaxis,/doyaxis,mtitle='Various Analysis Plots'

;; Panel 1

plot,rb3.vlsr,rb3.c_meas,psym=8,xtit='V!dLSR!n [km/s]',ytit='C'
oplot,rb3[funny].vlsr,rb3[funny].c_meas,psym=8,color=cgColor('Deep Pink')

multiplot,/doxaxis,/doyaxis

;; Panel 2

plot,nearfar,rb3.c_meas,xr=[-1,2],xtit='NEAR/FAR',ytit='C',psym=8,/xst
oplot,nearfar[funny],rb3[funny].c_meas,psym=8,color=cgColor('Deep Pink')

multiplot,/doxaxis,/doyaxis

;; Panel 3

plot,fitdist/1.d3,rb3.c_meas,xtit='D!dIRDC!n [kpc]',ytit='C',psym=8,/nodata
oploterror,fitdist/1.d3,rb3.c_meas,rb3.sig_c,psym=8,errcolor='BLK4'
oplot,fitdist/1.d3,rb3.c_meas,psym=8
oplot,fitdist[funny]/1.d3,rb3[funny].c_meas,psym=8,color=cgColor('Deep Pink')

multiplot,/doxaxis,/doyaxis

;; Panel 4

plot,rb3.s_peak/1.d3,rb3.c_meas,psym=8,xtit='S!d1.1!n [Jy]',ytit='C',$
     /xlog,/nodata
oploterror,rb3.s_peak/1.d3,rb3.c_meas,rb3.s_err/1.d3,rb3.sig_c,psym=8,errcolor='BLK4'
oplot,rb3.s_peak/1.d3,rb3.c_meas,psym=8
oplot,rb3[funny].s_peak/1.d3,rb3[funny].c_meas,psym=8,color=cgColor('Deep Pink')

multiplot,/doxaxis,/doyaxis

;; Panel 5

plot,rb3.s_peak/1.d3,fitdist/1.d3,psym=8,xtit='S!d1.1!n [Jy]',$
     ytit='D!dIRDC!n [kpc]',/xlog
st = findgen(101)/50.
dt = -10*st+20.
oplot,st,dt,color=cgColor('Blue'),thick=2,linestyle=3
oplot,rb3[funny].s_peak/1.d3,fitdist[funny]/1.d3,psym=8,$
      color=cgColor('Deep Pink')

multiplot,/doxaxis,/doyaxis

;; Panel 6

TEMP_DEPENDENT_VARIABLES, res[4]
print,m4_stat(f_data)
print,m4_stat(sig_f)

ratio = abs(sig_f/f_data)
gind = where(ratio LE 20., ng) & print,n-ng
;; f_data = f_data[gind]
;; sig_f  = sig_f[gind]
;; rb3 = rb3[gind]

sigma = cgSymbol('sigma')

plot,sig_f[gind]/f_data[gind],rb3[gind].sig_c/rb3[gind].c_meas,psym=8,$
     xtit=sigma+'!df!n / f',ytit=sigma+'!dC!n / C',/ylog;,/xlog
oplot,sig_f[funny]/f_data[funny],rb3[funny].sig_c/rb3[funny].c_meas,$
      psym=8,color=cgColor('Deep Pink')
al_legend,/bottom,/right,[string(n-ng,format="(I0)")+' sources not shown'],$
          box=0

multiplot,/reset
multiplot,/default
multiplot,/reset
myps,/done

;;=================================================================
;; Page 2 of analysis plots

myps,'irdc_dist_model/analysis_plots/misc_analysis_plots2.eps',xsize=15,ysize=9
multiplot,[3,2],gap=0.04,/doxaxis,/doyaxis,mtitle='Various Analysis Plots 2'

;; Panel 1

plot,rb3.vlsr,fitdist/1.d3,psym=8,xtit='V!dLSR!n [km/s]',ytit='D!dIRDC!n [kpc]'
oplot,rb3[funny].vlsr,fitdist[funny]/1.d3,psym=8,color=cgColor('Deep Pink')

multiplot,/doxaxis,/doyaxis

;; Panel 2

plot,rb3.l,rb3.c_meas,psym=8,xtit='GLON [deg]',ytit='C',xr=[60,10]
oplot,rb3[funny].l,rb3[funny].c_meas,psym=8,color=cgColor('Deep Pink')

multiplot,/doxaxis,/doyaxis

;; Panel 3

plot,rb3.l,fitdist/1.d3,psym=8,xtit='GLON [deg]',ytit='D!dIRDC!n [kpc]',$
     xr=[60,10]
oplot,rb3[funny].l,fitdist[funny]/1.d3,psym=8,color=cgColor('Deep Pink')
ells = findgen(101)/2.+10.
tand = R0*cos(ells*!dtor)/1.d3
oplot,ells,tand,thick=3,color=cgColor('Dodger Blue'),linestyle=3

multiplot,/doxaxis,/doyaxis

;; Panel 4

plot,rb3.s_err/rb3.s_peak,rb3.sig_c/rb3.c_meas,psym=8,xtit=sigma+'!dS!n/S',$
     ytit=sigma+'!dC!n/C'
oplot,rb3[funny].s_err/rb3[funny].s_peak,rb3[funny].sig_c/rb3[funny].c_meas,$
      psym=8,color=cgColor('Deep Pink')

multiplot,/reset
multiplot,/default
multiplot,/reset
myps,/done



;; print,rb3[funny].cnum

;; frb = rb3[funny]
;; FOR jj=0L,nf-1 DO BEGIN
;;    spawn,'open local/output/ubc_examine'+$
;;          string(frb[jj].cnum,format="(I04)")+'.pdf'
;;    wait,1.
;; ENDFOR

END
